import { REACT_ELEMENT, REACT_TEXT } from './constants';

/**
 * 包装一层可能的虚拟DOM
 * @param {*} element 
 * @returns 
 */
export function wrapToVdom(element) {
  return typeof element === 'string' || typeof element === 'number'
    ? {
      $$typeof: REACT_ELEMENT, // 元素类型
      type: REACT_TEXT, // DOM 标签， h1,h2,span.div
      props: { content: element } // 属性 className， style， children
    }
    : element;
}
/**
 * 浅比较
 */
export function shallowEqual(obj1, obj2) {
  if (obj1 === obj2) {
    return true;
  }
  if (typeof obj1 != "object" || obj1 === null || typeof obj2 != "object" || obj2 === null) {
    return false;
  }

  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);

  if (keys1.length !== keys2.length) {
    return false;
  }
  for (const key of keys1) {
    // Object.prototype.hasOwnProperty.call(obj2, key)
    if (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) {
      return false;
    }
  }
  return true;
}